// 初始化莫凡工具类
import utilConfig from './config/index';
import utilCommon from './common/index';
import cloud from './cloud/index';
import Emitter from './lib/emitter';



class mfutil {
  // #config = {};
  constructor(options) {
    const { config = {} } = options || {};
    this.config = new utilConfig(config);
    this.emitter = new Emitter();
    this.authorizeState = false;
  }
  // 初始化
  async init(options) {
    options = options || {};
    if (!options.cloud) {
      throw new Error('cloud need!');
    }
    // 初始化云函数的方法
    this.cloud = new cloud({ cloud: options.cloud, that: this });
    // 获取手机信息
    this.mobileInfo = this._getMobileInfo();
    // 初始化所有的公共类
    await this._initAllModule(this.cloud, this.emitter);
    // 执行初始化类之后的方法
    await this._initPostposition();
  }

  // 初始化所有的模块
  async _initAllModule(_cloud, _emitter) {
    for (let i = 0; i < this.config.package.length; i++) {
      let util;
      try {
        let options = Object.assign({
          _cloud,
          _emitter,
          _that: this,
        }, this.config.package[i].parameter);
        util = new this.config.package[i].package(options);
      } catch (err) {
        console.log('模块初始化异常:', JSON.stringify(err));
      } finally {
        this[this.config.package[i].key] = util;
      }
    }
  }

  // 初始化的后置方法
  async _initPostposition() {
    const { logger, mobileInfo } = this;
    // 打印设备信息
    logger && logger.log(mobileInfo, "设备信息");
  }

  // 处理淘宝页面信息
  _getMobileInfo() {
    const myMobileInfo = my.getSystemInfoSync();
    const _mobileInfo = { ...myMobileInfo };
    // 官方判断是不是IDE
    _mobileInfo.isIDE = my.isIDE;
    return _mobileInfo
  }

  // 小部件 初始化
  onAppInit() {
    this.logger && this.common && this.logger.log(this.common.date.format(), 'onAppInit');
    for (let i = 0; i < this.config.package.length; i++) {
      if (this[this.config.package[i].key] && this[this.config.package[i].key].onAppInit) {
        this[this.config.package[i].key].onAppInit();
      }
    }
  }

  // 小部件模块出现
  onAppear(options) {
    this.logger && this.common && this.logger.log(this.common.date.format(), 'onAppear');
    for (let i = 0; i < this.config.package.length; i++) {
      if (this[this.config.package[i].key] && this[this.config.package[i].key].onAppear) {
        this[this.config.package[i].key].onAppear(options);
      }
    }
  }

  // 小部件模块消失在屏幕时触发
  onDisAppear() {
    this.logger && this.common && this.logger.log(this.common.date.format(), 'onDisAppear');
    for (let i = 0; i < this.config.package.length; i++) {
      if (this[this.config.package[i].key] && this[this.config.package[i].key].onDisAppear) {
        this[this.config.package[i].key].onDisAppear();
      }
    }
  }

  // 小部件 onShow
  onAppShow() {
    this.logger && this.common && this.logger.log(this.common.date.format(), 'onAppShow');
    for (let i = 0; i < this.config.package.length; i++) {
      if (this[this.config.package[i].key] && this[this.config.package[i].key].onAppShow) {
        this[this.config.package[i].key].onAppShow();
      }
    }
  }

  // 小部件 onShow
  onAppHide() {
    this.logger && this.common && this.logger.log(this.common.date.format(), 'onAppHide');
    for (let i = 0; i < this.config.package.length; i++) {
      if (this[this.config.package[i].key] && this[this.config.package[i].key].onAppHide) {
        this[this.config.package[i].key].onAppHide();
      }
    }
  }
}
export default mfutil;
